<?php
namespace app\admin\model;
use app\common\model\CommonModel;
use app\common\model\OrderActivity;
use app\common\model\OrderConsult;
use app\common\model\OrderAfter;
use app\common\model\OrderGoods;
use app\common\model\Order;
use app\common\model\Shop;
use think\Session;
use think\Db;

class AfterModel extends CommonModel
{
	/**
    * 获取订单列表
    */
    public static function getAfterList($info)
    {
        $where = self::is_where($info);
        $field = 'o.order_id,a.order_after_id,a.after_goods,o.order_sn,o.addtime,o.username,o.phone,o.user_sn,o.clerk,a.after_money';
        $orderList = (new OrderAfter())->afterList($where,2,$field);
        
        $orderList->each(function($item, $key){
            $item->ogoods = (new OrderGoods())->orderGoodsInfo(['order_goods_id'=>$item->after_goods],'*');
        });
        
        /*退货订单数*/
        $thdd = self::after_count($where,'count');
        /*退货总金额*/
        $thze = self::after_count($where,'sum','after_money');
        
        return [$orderList,$thdd,$thze];
    }
    /**
    * 条件整理
    */
	private function is_where($info)
	{
        /*售后类型*/
        
        if($info['ty'] != 2)
        {
            $where['after_type'] = ['IN','0,1'];
        }else{
            $where['after_type'] = 2;
        }
        /*产品属性*/
		if($info['attribute_name'] != '' && $info['attribute_name'])
		{
			$where['g.attribute_name'] = $info['attribute_name'];
		}
        /*产品品牌*/
		if($info['brand_name'] != '' && $info['brand_name'])
		{
			$where['g.brand_name'] = $info['brand_name'];
		}
        /*产品子分类*/
		if($info['cat_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['g.cat_id'] = $info['cat_id'];
		}
        /*产品分类*/
		if($info['category_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['g.category_id'] = $info['category_id'];
		}
        /*产品颜色*/
		if($info['colour_name'] != '' && $info['colour_name'])
		{
			$where['g.colour_name'] = $info['colour_name'];
		}
        /*产品内存*/
		if($info['memory_name'] != '' && $info['memory_name'])
		{
			$where['g.memory_name'] = $info['memory_name'];
		}
        /*店铺*/
		if($info['shop_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['a.shop_id'] = $info['shop_id'];
		}
        /*关键字搜索*/
		if($info['sreach'])
		{
			$where['o.order_sn'] = ['like','%'.$info['sreach'].'%'];
		}
        /*时间区间*/
        if($info['start'] != '' || $info['end'] != '')
		{
			if($info['start'])
			{
				$where['a.addtime'][] = ['>',strtotime($info['start'])];
			}
			if($info['end'])
			{
				$where['a.addtime'][] = ['<',strtotime($info['end'])];
			}
			return $where;
		}
        /*上月、当前月*/
        if(array_key_exists('shijian',$info))
        {
            if($info['shijian'] != '')
            {
                if($info['shijian'] == CommonModel::NUMBER_ZERO)
                {
                    $where['a.addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
                    $where['a.addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
                }else{
                    $where['a.addtime'][] = ['>',mktime(0, 0, 0,date('m')-1,1,date('Y'))];
                    $where['a.addtime'][] = ['<',mktime(0, 0, 0,date('m'),0,date('Y'))];
                }
            }
        }else{
            $where['a.addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
            $where['a.addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
        }
		return $where;
	}
    /**
    * 获取列表信息
    */
    public static function getConsultList($info)
    {
        $where = self::is_where1($info);
        $field = 'o.order_consult_id,o.addtime,o.username,o.phone,o.user_sn,g.goods_name,g.is_stock';
        $data = (new OrderConsult())->getConsultList($where,$field,0,$info['page']);
        foreach($data['data'] as $i=>$vo)
        {
            $data['data'][$i]['addtime'] = date('Y-m-d H:i:s',$vo['addtime']);
            $data['data'][$i]['is_stock'] = $vo['is_stock']==0?'无货':'有货';
        }
        return $data;
    }
    /**
    * 条件整理
    */
	private function is_where1($info)
	{
        /*产品属性*/
		if($info['attribute_name'] != '' && $info['attribute_name'])
		{
			$where['g.attribute_name'] = $info['attribute_name'];
		}
        /*产品品牌*/
		if($info['brand_name'] != '' && $info['brand_name'])
		{
			$where['g.brand_name'] = $info['brand_name'];
		}
        /*产品子分类*/
		if($info['cat_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['g.cat_id'] = $info['cat_id'];
		}
        /*产品分类*/
		if($info['category_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['g.category_id'] = $info['category_id'];
		}
        /*产品颜色*/
		if($info['colour_name'] != '' && $info['colour_name'])
		{
			$where['g.colour_name'] = $info['colour_name'];
		}
        /*产品内存*/
		if($info['memory_name'] != '' && $info['memory_name'])
		{
			$where['g.memory_name'] = $info['memory_name'];
		}
        /*关键字搜索*/
		if($info['sreach'])
		{
			$where['g.goods_name'] = ['like','%'.$info['sreach'].'%'];
		}
        /*店铺*/
		if($info['shop_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['o.shop_id'] = $info['shop_id'];
		}
        /*时间区间*/
        if($info['start'] != '' || $info['end'] != '')
		{
			if($info['start'])
			{
				$where['o.addtime'][] = ['>',strtotime($info['start'])];
			}
			if($info['end'])
			{
				$where['o.addtime'][] = ['<',strtotime($info['end'])];
			}
			return $where;
		}
        /*上月、当前月*/
        if(array_key_exists('shijian',$info))
        {
            if($info['shijian'] != '')
            {
                if($info['shijian'] == CommonModel::NUMBER_ZERO)
                {
                    $where['o.addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
                    $where['o.addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
                }else{
                    $where['o.addtime'][] = ['>',mktime(0, 0, 0,date('m')-1,1,date('Y'))];
                    $where['o.addtime'][] = ['<',mktime(0, 0, 0,date('m'),0,date('Y'))];
                }
            }
        }else{
            $where['o.addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
            $where['o.addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
        }
		return $where;
	}
    /********************订单统计数据**********************/
    /**
    * 统计数据
    */
    private function after_count($where,$ty,$field = '')
    {
        if($ty == 'count')
        {
           return (new OrderAfter())->afterCount($where);
        }
        return (new OrderAfter())->afterSum($where,$field);
    }
}

